<template>
  <div>
    <el-form :inline="true" ref="searchForm" :model="searchMap" class="demo-form-inline">
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="供应商名称"></el-input>
      </el-form-item>
      <el-form-item prop="linkman" v-if="!isgood">
        <el-input v-model="searchMap.linkman" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item prop="phone" v-if="!isgood">
        <el-input v-model="searchMap.phone" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="add" v-if="!isgood">新增</el-button>
        <el-button @click="resetForm('searchForm')" v-if="!isgood">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :highlight-current-row="isgood"
      @current-change="handleChange"
      :data="list"
      border
      style="width: 100% "
      height="470px"
    >
      <el-table-column type="index" align="center" label="序号" width="60"></el-table-column>
      <el-table-column prop="name" label="供应商名称" width="180"></el-table-column>
      <el-table-column prop="linkman" label="联系人" width="180"></el-table-column>
      <el-table-column prop="phone" label="联系电话" v-if="!isgood"></el-table-column>
      <el-table-column prop="remark" label="备注" v-if="!isgood"></el-table-column>
      <el-table-column label="操作" width="160" v-if="!isgood">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="size"
      :layout="!isgood?'total, sizes, prev, pager, next, jumper':'prev, pager, next'"
      :total="total"
    ></el-pagination>
    <el-dialog title="供应商编辑" :visible.sync="dialogFormVisible" width="500px">
      <el-form :rules="rules" :model="addForm" style="width:450px" ref="addForm">
        <el-form-item label="供应商名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="addForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人" :label-width="formLabelWidth" prop="linkman">
          <el-input v-model="addForm.linkman" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" :label-width="formLabelWidth" prop="phone">
          <el-input v-model="addForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
          <el-input v-model="addForm.remark" type="textarea" rows="2" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addForm.id===null?addDate('addForm'):editDate('addForm')"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import supplierApi from "@/api/supplier";
export default {
  data() {
    return {
      list: [],
      page: 1,
      size: 10,
      searchMap: {
        name: "",
        linkman: "",
        phone: ""
      },
      dialogFormVisible: false,
      formLabelWidth: "100px",
      addForm: {
        id: null,
        name: "",
        linkman: "",
        phone: "",
        remark: ""
      },
      total: 0,
      rules: {
        name: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        linkman: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ]
      }
    };
  },
  props: { isgood: Boolean },

  components: {},
  created() {
    // this.getList();
    this.search();
  },

  methods: {
    getList() {
      supplierApi.getList().then(res => {
        const resp = res.data;
        if (resp.flag) {
          this.list = resp.data;
        } else {
          this.$message({
            message: "获取数据失败",
            type: "error"
          });
        }
      });
    },
    handleChange(val) {
      console.log(val)
      this.$emit("select",val);
    },
    search() {
      supplierApi.search(this.page, this.size, this.searchMap).then(res => {
        const resp = res.data;
        if (resp.flag) {
          this.list = resp.data.rows;
          this.total = resp.data.total;
        } else {
          this.$message({
            message: "获取数据失败",
            type: "error"
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    add() {
      this.dialogFormVisible = true;
      // this.$nextTick()它是一个异步事件，当渲染结束 之后 ，它的回调函数才会被执行
      this.$nextTick(() => {
        this.$refs["addForm"].resetFields();
      });
    },
    addDate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          supplierApi.addDate(this.addForm).then(res => {
            const resp = res.data;
            if (resp.flag) {
              this.$message({
                message: "新增供应商成功",
                type: "success"
              });
              this.dialogFormVisible = false;
              this.search();
            } else {
              //新增失败
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleEdit(id) {
      console.log("编辑", id);
      this.add();
      supplierApi.getById(id).then(res => {
        const resp = res.data;
        if (resp.flag) {
          this.addForm = resp.data;
        } else {
          console.log("查询失败");
        }
      });
    },
    editDate(formName) {
      this.$refs[formName].validate(val => {
        if (val) {
          supplierApi.edit(this.addForm).then(res => {
            const resp = res.data;
            if (resp.flag) {
              this.$message({
                message: "编辑供应商成功",
                type: "success"
              });
              this.dialogFormVisible = false;
              this.search();
            } else {
              this.$message({
                message: "编辑失败",
                type: "error"
              });
            }
          });
        }
      });
    },
    handleDelete(id) {
      console.log("删除", id);
      this.$confirm("此操作将永久删除该供应商, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          supplierApi.delete(id).then(res => {
            const resp = res.data;
            if (resp.flag) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.search();
            } else {
              this.$message({
                type: "error",
                message: "删除失败!"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSizeChange(val) {
      this.size = val;
      this.search();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.search();
    }
  }
};
</script>

<style scoped>
</style>